package com.rykj.qxj.monitor.core;

import com.alibaba.druid.support.json.JSONUtils;
import com.rykj.qxj.server.util.SmsUtil;
import com.rykj.qxj.server.util.HttpClientUtils;
import com.rykj.qxj.server.util.date.DateUtils;
import lombok.extern.log4j.Log4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 每天统计
 */
@Component
@Log4j
public class AliyunGjHandle {

//    @Scheduled(cron = "0 */2 * * * ?")
    public static void statisPageView(){
        log.info("阿里云平台告警监测.....");
        long startTime = DateUtils.addHowMinute(new Date(),-2).getTime();
//        long startTime = 1565084688400L;
        String url = "http://cn-shenzhen-szqx-d01-plat-monitor.aso.ops.szqx.com:8092/aso/fullview/event/list" +
                "?service=&region=&level=&product=&status=&timeStart="+startTime+"&timeEnd="+System.currentTimeMillis()+"&currentPage=1&pageSize=15";
        try {
            String result = HttpClientUtils.get(url);
            Map map = (Map) JSONUtils.parse(result);
            if(map.get("code").equals("200")){
               Map data = (Map) map.get("data");
               int total = Integer.parseInt(data.get("total").toString());
               if(total>0){
                 List<Map<String,String>> list = (List<Map<String,String>>) data.get("result");
                 for(Map alarm : list){
                    String eventLevel = (String) alarm.get("eventLevel"); //告警等级是紧急才报警
                     if(eventLevel.equals("FATAL")){ //紧急
                         //短信通知
                         String content = (String) alarm.get("message");
                         SmsUtil.sendDX("15016710125","阿里云平台告警:" + content);
                         log.error("阿里云平台告警:" + content);
                     }
                 }
               }else {
                   log.info("暂无告警信息");
               }
            }else{
                log.error("aliyun告警接口异常，请检查");
            }
            System.out.println(map.get("code"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        statisPageView();
    }
}
